package com.victor9ac.mashup.core.adapter;

import java.io.IOException;
import java.net.URISyntaxException;

import org.apache.http.client.ClientProtocolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.victor9ac.ip.finder.IpAddressValidator;
import com.victor9ac.ip.finder.IpFinder;




@Component
public class TravelCalculator {

	
	@Autowired
	IpFinder ipFinder;
	@Autowired
	IpAddressValidator ipAddressValidator;
	
	String buildTravelFrom(String from) {
		if (ipAddressValidator.isIP(from)){
			from = determintateOrigin(from);
		}
		return from;
	}

	


	private String determintateOrigin(String from) {
		String cityFrom = IpFinder.UNSUCESSFUL_LOCATION_IP;
		try {
			cityFrom = ipFinder.getLocationFromIp(from);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (IpFinder.UNSUCESSFUL_LOCATION_IP.equals(cityFrom)){
			cityFrom = null;
		}
		return cityFrom;
	}

	
}
